const webpack = require("webpack");

//用于动态导出html文件
const HtmlWebpackPlugin = require('html-webpack-plugin')

//独立打包style文件、单入口文件、会把所有的less文件、打包到一个文件
var ExtractTextPlugin = require('extract-text-webpack-plugin')

//当前项目环境
const DEVELOPMENT=process.env.NODE_ENV==='development';
const PRODUCTION=process.env.NODE_ENV==='production';



function root(key){
    return __dirname + "/"+key;
}


module.exports={
    context: root("src"),

    devServer: {
        port: 7777,
        host: 'localhost',
        contentBase: root("src"),
        proxy: {
            '/api': {
                target: 'https://api.seniverse.com',
                pathRewrite: {'^/api': '/'},
                changeOrigin: true,
                secure: false
            }
        }
    },

    entry: {
        app: "./index.js"
    },
    output: {
        path: root("dist"),
        filename: "[name].[hash].js"
    },

    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: [{
                    loader: "babel-loader"
                }],
            },
            {
                test: /\.css$/,
                exclude: /node_modules|static/,
                loader: 'style-loader!css-loader'
            },
            {
                test: /\.less$/,
                exclude: /node_modules|static/,
                loader: ExtractTextPlugin.extract({loader: 'css-loader!less-loader'})
            },
            {
                test: /\.(png|jpg|gif|md)$/,
                use: ['url-loader?limit=8192&name=images/[name].[ext]']
            }
        ]
    },

    plugins: [
        new ExtractTextPlugin("[name].[hash].css"),
        //自动生成html
        new HtmlWebpackPlugin({
            filename: "index.html",
            template: 'template.html'
        }),
        new webpack.optimize.CommonsChunkPlugin({
            name: "commons",
            filename: "commons.js",
            minChunks: 2
        })
    ]
}
